//
//  AXFShoppingButtonView.m
//  AXFEightGroup
//
//  Created by ya on 12/24/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFShoppingButtonView.h"

@interface AXFShoppingButtonView ()

@property (weak, nonatomic) IBOutlet UILabel        *amountLabel;
@property (weak, nonatomic) IBOutlet UIButton       *minusButton;
@property (weak, nonatomic) IBOutlet UIButton       *plusButton;
@property (nonatomic, assign) NSInteger             count;


@end

@implementation AXFShoppingButtonView

- (void)awakeFromNib {
    [super awakeFromNib];

    [self configurationObsever];
}


- (NSInteger)maxCount {
    return self.model.number.integerValue;
}

- (IBAction)actionPlusButton:(UIButton *)sender {
    if ( (self.count + 1) >= self.maxCount ) {
        [self showMaxMessage];
        self.count = self.maxCount;
        return;
    }
    self.count ++;
}

- (IBAction)actionMinusButton:(UIButton *)sender {
    if ( self.count < 0 ) {
        self.count = 0;
        return;
    }
    self.count --;
}











- (UIView *)currentCell {
    UIView *superView = self;

    while ( (superView = superView.superview) ) {

        if ( [superView isKindOfClass:[UITableViewCell class]] ) {
            return superView;
        }

        if ( [superView isKindOfClass:[UICollectionViewCell class]] ) {
            return superView;
        }
    }

    return nil;
}




#pragma mark -


#pragma mark - 监听

#define ModelKey    @"model"
#define CountKey    @"count"

- (void)configurationObsever {
    [self addObserver:self
           forKeyPath:ModelKey
              options:NSKeyValueObservingOptionNew
              context:nil];

    [self addObserver:self
           forKeyPath:CountKey
              options:NSKeyValueObservingOptionNew
              context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey,id> *)change
                       context:(void *)context {


    if ([keyPath isEqualToString:ModelKey]) {

        AXFDataModel *model = change[NSKeyValueChangeNewKey];
        self.count = model.productAmount;
    }



    if ([keyPath isEqualToString:CountKey]) {


        NSInteger count = self.count;

        UIView *cell = self.currentCell;

        if ( _model.productAmount < count ) {
            NotificationCenterPost(Action_AXFShoppingButtonView_Plus,
                                   cell,
                                   nil);
        }
        else if ( _model.productAmount > count ) {
            NotificationCenterPost(Action_AXFShoppingButtonView_Miuns,
                                   cell,
                                   nil);
        }

        _model.productAmount = count;

        count == 0 ? [_ModelMgr productListRemoveModel:_model] : [_ModelMgr productListAddModel:_model];

        NotificationCenterPost( Action_AXFShoppingButtonView_AmountChange,
                                cell,
                                nil );

    }



    /// 任何时候都刷新UI
    [self refreshUI];

}



- (void)refreshUI {



    self.amountLabel.text   = NSStringFromInteger(self.model.productAmount);
    self.minusButton.hidden = self.model.productAmount == 0 ? YES : NO;
    self.amountLabel.hidden = self.minusButton.hidden;
    self.plusButton.enabled = self.model.number.integerValue == self.model.productAmount ? NO : YES;
}


- (void)dealloc {
    [self removeObserver:self forKeyPath:ModelKey];
    [self removeObserver:self forKeyPath:CountKey];
}

#pragma mark -

- (void)showMaxMessage {

    NSString *showMessage = nil;

    if (_model.name == nil) {
        showMessage = @" 库存不足了, 过段时间再来看看吧";
    }
    else {
        showMessage = _model.name.link(@" 库存不足了, 过段时间再来看看吧");
    }

    [SVProgressHUD showImage:[UIImage originImage:@"notsupporttakeself"] status:showMessage];
}


- (CGSize)intrinsicContentSize {
    return CGSizeMake(85.0, 30.0);
}



@end

